package ch.ielse.demo.p02;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * QQ 517309507
 * 至尊流畅;daLao专用;/斜眼笑
 */
public class MessagePicturesLayout extends FrameLayout implements View.OnClickListener {

    public static final int MAX_DISPLAY_COUNT = 9;
    private final FrameLayout.LayoutParams lpChildImage = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    private final int mSingleMaxSize;
    private final int mSpace;
    private final List<ImageView> iPictureList = new ArrayList<>();
    private final SparseArray<ImageView> mVisiblePictureList = new SparseArray<>();
    private final TextView tOverflowCount;

    private Callback mCallback;
    private boolean isInit;
    private List<Uri> mDataList;
    private List<Uri> mThumbDataList;

    private int mMaxSize = 216;
    private int mSpaceSize = 5;

    public MessagePicturesLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        DisplayMetrics mDisplayMetrics = context.getResources().getDisplayMetrics();

        // 只有一个时候的最大显示尺寸
        mSingleMaxSize = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mMaxSize, mDisplayMetrics) + 0.5f);
        // 间距大小
        mSpace = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mSpaceSize, mDisplayMetrics) + 0.5f);

        // 添加图片imageview
        for (int i = 0; i < MAX_DISPLAY_COUNT; i++) {
            ImageView squareImageView = new SquareImageView(context);
            squareImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            squareImageView.setVisibility(View.GONE);
            squareImageView.setOnClickListener(this);
            addView(squareImageView);
            iPictureList.add(squareImageView);
        }

        // 大于9时的个数
        tOverflowCount = new TextView(context);
        tOverflowCount.setTextColor(0xFFFFFFFF);
        tOverflowCount.setTextSize(24);
        tOverflowCount.setGravity(Gravity.CENTER);
        tOverflowCount.setBackgroundColor(0x66000000);
        tOverflowCount.setVisibility(View.GONE);
        addView(tOverflowCount);
    }

    public void set(List<Uri> urlThumbList, List<Uri> urlList) {
        mThumbDataList = urlThumbList;
        mDataList = urlList;
        if (isInit) {
            notifyDataChanged();
        }
    }

    /**
     *  主要内容布局的结构
     */
    private void notifyDataChanged() {
        // 封面地址
        final List<Uri> thumbList = mThumbDataList;
        // 封面地址个数
        final int urlListSize = thumbList != null ? mThumbDataList.size() : 0;

        // 没有封面地址时, 不显示该布局
        if (thumbList == null || thumbList.size() < 1) {
            setVisibility(View.GONE);
            return;
        } else {
            setVisibility(View.VISIBLE);
        }

        // 封面地址大于详情地址-->报错
        if (thumbList.size() > mDataList.size()) {
            throw new IllegalArgumentException("dataList.size(" + mDataList.size() + ") > thumbDataList.size(" + thumbList.size() + ")");
        }

        // 根据封面地址-->分为几列
        int column = 3;
        if (urlListSize == 1) {
            column = 1;
        } else if (urlListSize == 4 || urlListSize== 2) {
            column = 2;
        }

        // 根据封面地址-->分为几行
        int row = 0;
        if (urlListSize > 6) {
            row = 3;
        } else if (urlListSize > 3) {
            row = 2;
        } else if (urlListSize > 0) {
            if (urlListSize == 2){
                row = 2;
            }else {
                row = 1;
            }
        }

        // TODO: 2019/5/11 设置两张图片时的大小
        // 设置多张图片的尺寸
        int imageSize = 0;
        if (urlListSize == 1){
            // 只有一张时,单张图片的size
            imageSize = mSingleMaxSize;
        }else if (urlListSize == 2){
            // 两张张图片时,一张图片的size
            int width = getWidth(); // frameLayout的宽度
            imageSize = (int) ((getWidth() * 1f - mSpace * (column - 1)) * 0.6);
        }else {
            // 多张图片时,一张图片的size
            int width = getWidth(); // frameLayout的宽度
            imageSize = (int) ((getWidth() * 1f - mSpace * (column - 1)) / column);
        }

        // 单张图片的layoutParam
        lpChildImage.width = imageSize;
        // 设置高度 = 宽度 * 0.6
        lpChildImage.height = (int) (lpChildImage.width * 0.6);

        // 多出图片的个数的 textview
        tOverflowCount.setVisibility(urlListSize > MAX_DISPLAY_COUNT ? View.VISIBLE : View.GONE);
        tOverflowCount.setText("+ " + (urlListSize - MAX_DISPLAY_COUNT));
        tOverflowCount.setLayoutParams(lpChildImage);
        mVisiblePictureList.clear();
        for (int i = 0; i < iPictureList.size(); i++) {
            final ImageView iPicture = iPictureList.get(i);
            iPicture.setBackground(getResources().getDrawable(R.drawable.radio_fff_5));
            FrameLayout.LayoutParams params = (LayoutParams) iPicture.getLayoutParams();
            params.setMargins(10,10,10,10);
            if (i < urlListSize) {
                iPicture.setVisibility(View.VISIBLE);
                mVisiblePictureList.put(i, iPicture);
                iPicture.setLayoutParams(lpChildImage);
                iPicture.setBackgroundResource(R.drawable.default_picture);
                Glide.with(getContext()).load(thumbList.get(i)).into(iPicture);
                // 平移图片位置
                if (urlListSize == 8 && (i == 6 || i == 7)){
                    iPicture.setTranslationX((i % column) * (imageSize + mSpace) + imageSize/2);
                }else if (urlListSize == 5 && (i == 3 || i == 4)){
                    iPicture.setTranslationX((i % column) * (imageSize + mSpace) + imageSize/2);
                }else if (urlListSize == 2 &&  i == 1){
                    iPicture.setTranslationX((0 % column) * (imageSize + mSpace));
                }else {
                    iPicture.setTranslationX((i % column) * (imageSize + mSpace));
                }

                // 设置高度 = 宽度 * 0.6
                if (urlListSize == 2 && i == 1){
                    iPicture.setTranslationY((float) ((3 / column) * (imageSize * 0.6 + mSpace)));
                }else {
                    iPicture.setTranslationY((float) ((i / column) * (imageSize * 0.6 + mSpace)));
                }
            } else {
                iPicture.setVisibility(View.GONE);
            }

            // 设置textview位置
            if (i == MAX_DISPLAY_COUNT - 1) {
                tOverflowCount.setTranslationX((i % column) * (imageSize + mSpace));
                tOverflowCount.setTranslationY((i / column) * (imageSize + mSpace));
            }
        }

        // 设置高度 = 宽度 * 0.6
        getLayoutParams().height = (int) (imageSize * row * 0.6 + mSpace * (row - 1));
    }

    // squareImageView 的点击事件
    @Override
    public void onClick(View v) {
        if (mCallback != null) {
            mCallback.onThumbPictureClick((ImageView) v, mVisiblePictureList, mDataList);
        }
    }

    public interface Callback {
        void onThumbPictureClick(ImageView i, SparseArray<ImageView> imageGroupList, List<Uri> urlList);
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        isInit = true;
        // 尺寸改变的时候,重新加载布局
        notifyDataChanged();
    }
}
